<template>
  <div class="login-container">
  <div class="login-form-wrap">
    <div class="login-head">
      <div class="logo"></div>
    </div>
    <!-- 表单 -->
    <el-form ref="form" :model="form" status-icon :rules="rules">
      <el-form-item prop="mobile">
        <el-input placeholder="请输入手机号码"  v-model="form.mobile"></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input placeholder="请输入验证码" v-model="form.code"></el-input>
      </el-form-item>
      <el-form-item prop="isRead">
        <el-checkbox label="我已阅读用户协议和隐私条款" v-model="form.isRead"></el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="login-btn" @click="login('form')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    const isRead = (rule, value, callback) => {
      value ? callback() : callback(new Error('请阅读隐私协议'))
    }
    return {
      form: {
        mobile: '13911111111',
        code: '246810',
        isRead: false
      },
      rules: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1\d{10}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '请输入正确的验证码', trigger: 'blur' }
        ],
        isRead: [
          { validator: isRead, trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    login (form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.$axios.post('/mp/v1_0/authorizations', this.form).then(msg => {
            this.$message.success('登陆成功')
          }).catch(() => {
            this.$message.error('登陆失败')
          })
        } else {
          alert('验证失败')
          return false
        }
      })
    }
  }
}
</script>

<style lang='less'>
.login-container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("~@/assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    width: 400px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("~@/assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
